﻿using System;
using System.Reflection;
using Microsoft.Practices.Unity;

namespace TestByWire.Factories.StubbingStrategies
{
	internal class ValueTypeAndStringArrayStrategy : StubStrategyBase
	{
		public override bool RegisterOnContainer
		{
			get { return false; }
		}

		public override bool CanHandleType(Type type, string instanceName)
		{
			if (type == null)
			{
				throw new ArgumentNullException("type");
			}

			return type.IsArray && type.HasElementType && (type.GetElementType().IsValueType || type.GetElementType() == typeof(string));
		}

		public override bool CompletesBuild
		{
			get
			{
				return false;
			}
		}

		protected override object TryCreateInstance(Type type)
		{
			if (type.GetElementType() == typeof(string))
			{
				return new string[0];
			}

			var arrayType = type.GetElementType().MakeArrayType();

			return Activator.CreateInstance(arrayType, 0);
		}
	}
}
